home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GLIQ / SCORE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  5.7 KB  |  278 lines

  1. /*  
  2.  *  CS 453 - Final project : An OpenGL version of the pegboard game IQ
  3.  *  Due : June 5, 1997
  4.  *  Author : Kiri Wagstaff
  5.  *
  6.  *  File : score.c
  7.  *  Description : Maintains and adds to highscores.
  8.  *
  9.  */
  10.  
  11. #include "gliq.h"
  12.  
  13. int scores[10];
  14. int totpegs[10];
  15. char inits[10][4];
  16. int minscore=0;
  17. int minpegs=100;
  18. char newinits[3];
  19. int numentered=0;
  20. int written=0;
  21. float color1=1.0, color2=1.0, color3=0.0;
  22. int lasthigh=-1;
  23.  
  24. void highscore(void);
  25. void readscores(void);
  26. void showhighscores(void);
  27. void keyscores(unsigned char key, int x, int y);
  28.  
  29. void highscore(void)
  30. {
  31.   int i, j;
  32.   int width = glutGet(GLUT_WINDOW_WIDTH);
  33.   int height = glutGet(GLUT_WINDOW_HEIGHT);
  34.   FILE* fp;
  35.   
  36.   /* Prompt for initials */
  37.   glColor3f(1.0, 1.0, 0.0); /* yellow */
  38.   text(0.08*width, 0.85*height, 0.1*height, "CONGRATULATIONS!");
  39.   glColor3f(1.0, 0.0, 0.0); /* red */
  40.   text(0.05*width, 0.7*height, 0.07*height, "You made it into the top 10!");
  41.   glColor3f(1.0, 1.0, 0.0); /* yellow */
  42.   text(0.2*width, 0.55*height, 0.07*height, "%02d remaining of %02d",
  43.        pegs, totalpegs);
  44.   glColor3f(0.0, 0.0, 1.0); /* blue */
  45.   text(0.13*width, 0.4*height, 0.07*height, "Please enter your initials:");
  46.  
  47.   /* Display what's been entered */
  48.   glColor3f(color1, color2, color3);
  49.   for (i=0; i<numentered; i++)
  50.     text((0.4+i/10.0)*width, 0.2*height, 0.2*height, "%c", newinits[i]);
  51.  
  52.   if (!written && numentered == 3)
  53.     {
  54. #if 0
  55. //      printf("Saving to file scores.txt...\n");
  56. #endif
  57.       for (i=0; i<10; i++)
  58.     if (scores[i]==-1 || 
  59.         (pegs<scores[i] || (pegs==scores[i] && totalpegs>totpegs[i])))
  60.       break;
  61.       for (j=9; j>i; j--)
  62.     {
  63.       if (scores[j-1]==-1 || scores[j-1]==0)
  64.         continue;
  65. #if 0
  66. //      printf("compare : ");
  67. //      printf(" %s      %02d     %02d\n", inits[j], scores[j], totpegs[j]);
  68. #endif
  69.       scores[j] = scores[j-1];
  70.       totpegs[j] = totpegs[j-1];
  71.       inits[j][0] = inits[j-1][0];
  72.       inits[j][1] = inits[j-1][1];
  73.       inits[j][2] = inits[j-1][2];
  74.       inits[j][3] = inits[j-1][3];
  75. #if 0
  76. //      printf("with : ");
  77. //      printf(" %s      %02d     %02d\n", inits[j], scores[j], totpegs[j]);
  78. #endif
  79.     }
  80. #if 0
  81. //      printf("Storing in index %d\n", i);
  82. #endif
  83.       lasthigh=i;
  84.       scores[i] = pegs;
  85.       totpegs[i] = totalpegs;
  86.       inits[i][0] = newinits[0];
  87.       inits[i][1] = newinits[1];
  88.       inits[i][2] = newinits[2];
  89.       inits[i][3] = 0;
  90.  
  91.       /* get the new min */
  92.       for (j=9; j>0; j--)
  93.     if (scores[j]==-1 || scores[j]==0)
  94.       continue;
  95.     else
  96.       {
  97.         minscore = scores[j];
  98.         minpegs = totpegs[j];
  99.         break;
  100.       }
  101. #if 0
  102. //      printf("New minscore %d, minpegs %d\n", minscore, minpegs);
  103. #endif
  104.       fp = fopen("scores.txt", "w");
  105.       if (!fp)
  106.     {
  107.       printf("Could not open scores.txt, exiting.\n");
  108.       exit(1);
  109.     }
  110.       for (i=0; i<10; i++)
  111.     if (scores[i]!=-1 && scores[i]!=0)
  112.       fprintf(fp, "%02d  %02d  %s\n", scores[i], totpegs[i], inits[i]);
  113.     else
  114.       break;
  115.       written=1;
  116.     }
  117.       
  118. }
  119.  
  120. void readscores(void)
  121. {
  122.   int i;
  123.   FILE* fp;
  124.   
  125.   newinits[0] = 0;
  126.   newinits[1] = 0;
  127.   newinits[2] = 0;
  128.   
  129.   /* Read in the current high scores */
  130.   fp = fopen("scores.txt", "r");
  131.   if (!fp)
  132.     {
  133.       printf("Could not open scores.txt, exiting.\n");
  134.       exit(1);
  135.     }
  136.   for (i=0; i<10; i++)
  137.     {
  138.        /* Pegs remaining */
  139.       if ((fscanf(fp, "%d", &(scores[i])))!=1)
  140.     {
  141.       scores[i] = -1;
  142.       break;
  143.     }
  144.       /* Total pegs */
  145.       if ((fscanf(fp, "%d", &(totpegs[i])))!=1)
  146.     {
  147.       totpegs[i] = -1;
  148.       break;
  149.     }
  150.       fscanf(fp, "%s", inits[i]);
  151. #if 0
  152. //      printf("read %s\n", inits[i]);
  153. #endif
  154.     }
  155.   if (i>0)
  156.     {
  157.       minscore = scores[i-1];
  158.       minpegs = totpegs[i-1];
  159.     }
  160.   
  161.   if (i<10)
  162.     {
  163.       minscore=100;
  164.       minpegs=0;
  165.     }
  166. #if 0
  167. //  printf("Minscore is %d, minpegs is %d\n", minscore, minpegs);
  168. #endif
  169. }
  170.  
  171. void showhighscores(void)
  172. {
  173.   int i;
  174.   int width = glutGet(GLUT_WINDOW_WIDTH);
  175.   int height = glutGet(GLUT_WINDOW_HEIGHT);
  176.   
  177.   /* Display the current highs */
  178.   glColor3f(1.0, 1.0, 0.0);  /* yellow */
  179.   text(0.15*width, 0.9*height, 0.07*height, "Initials  Score  Out of");
  180.   for (i=0; i<10; i++)
  181.     {
  182.       if (i>=1)
  183.     glColor3f(1.0, 0.0, 0.0); /* red */
  184.       else if (i>=5)
  185.     glColor3f(0.0, 0.0, 1.0); /* blue */
  186.       if (scores[i]>0)
  187.     {
  188.       if (i==lasthigh)
  189.         glColor3f(color1, color2, color3);
  190.       text(0.15*width, (8.0-0.65*i)/10.0*height, 0.05*height,
  191.            " %s", inits[i]);
  192.       text(0.48*width, (8.0-0.65*i)/10.0*height, 0.05*height,
  193.            "%02d", scores[i]);
  194.       text(0.75*width, (8.0-0.65*i)/10.0*height, 0.05*height,
  195.            "%02d", totpegs[i]);
  196.     }
  197.    }
  198.   glColor3f(color1, color2, color3);
  199.   text(0.15*width, 0.1*height, 0.07*height, "Click to continue...");
  200.  
  201. }
  202.  
  203. /* ARGSUSED1 */
  204. void keyscores(unsigned char key, int x, int y)
  205. {
  206. #if 0
  207.   if (key == '\r') /*return*/ {
  208.  
  209.   } else if (key == '\b') /*backspace*/ {
  210.   }
  211. #endif
  212.   if (numentered>=3)
  213.     return;
  214.   newinits[numentered] = key;
  215.   numentered++;
  216. #if 0
  217. //  printf("Read a %c\n", key);
  218. #endif
  219.   glutPostRedisplay();
  220. }
  221.  
  222. void idlescore(void)
  223. {
  224.   static int hscolor=0;
  225.  
  226.   switch(hscolor)
  227.     {
  228.     case 0:
  229.       color1=1.0;
  230.       color2=0.0;
  231.       color3=0.0;
  232.       hscolor++;
  233.       break;
  234.     case 1:
  235.       color1=0.5;
  236.       color2=0.5;
  237.       color3=0.0;
  238.       hscolor++;
  239.       break;
  240.     case 2:
  241.       color1=1.0;
  242.       color2=1.0;
  243.       color3=0.0;
  244.       hscolor++;
  245.       break;
  246.     case 3:
  247.       color1=0.0;
  248.       color2=1.0;
  249.       color3=0.0;
  250.       hscolor++;
  251.       break;
  252.     case 4:
  253.       color1=0.0;
  254.       color2=0.0;
  255.       color3=1.0;
  256.       hscolor++;
  257.       break;
  258.     case 5:
  259.       color1=1.0;
  260.       color2=0.0;
  261.       color3=1.0;
  262.       hscolor=0;
  263.       break;
  264.     }
  265.  
  266.   if (curstate==HIGHSC)
  267.     highscore();
  268.   else if (curstate==VIEWSCORES)
  269.     showhighscores();
  270.   else
  271.     {
  272.       printf("Unknown state %d, exiting\n", curstate);
  273.       exit(1);
  274.     }
  275.  
  276.   glutPostRedisplay();
  277. }
  278.